{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Thread\n",
    "\n",
    "本文档描述 Assistants API 中 与 Thread 相关的接口。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境准备\n",
    "\n",
    "首先需要安装AppBuilder-SDK代码库，若已在开发环境安装，则可跳过此步。\n",
    "\n",
    "**注意：**: appbuilder-sdk 的python版本要求 3.9+，安装的SDK version >= 0.7.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "!python3 -m pip install appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import appbuilder\n",
    "\n",
    "# 配置你的密钥，主要在这之前需要首先申请Assistant API的内测资格\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"your_appbuilder_token\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Thread相关函数\n",
    "\n",
    "### 创建Thread\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "创建Thread，返回对应的thread_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='thread_cdfea60ae56a4d5abebf29080f9a8bf7' object='thread' created_at=1717645470924 metadata={}\n"
     ]
    }
   ],
   "source": [
    "from appbuilder.core.assistant.type import thread_type\n",
    "\n",
    "# 创建一个对话thread\n",
    "message = thread_type.AssistantMessage(\n",
    "            content=\"hello world\"\n",
    ")\n",
    "thread = appbuilder.assistant.threads.create([message])\n",
    "\n",
    "# 打印创建的thread的信息-thread_id\n",
    "print(thread)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询Thread\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据thread_id查询Thread对象的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='thread_cdfea60ae56a4d5abebf29080f9a8bf7' object='thread' created_at=1717645470924 metadata={}\n"
     ]
    }
   ],
   "source": [
    "# 根据thread_id查询thread信息\n",
    "thr_query = appbuilder.assistant.threads.query(thread_id=thread.id)\n",
    "\n",
    "# 打印查询到的thread信息\n",
    "print(thr_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改Thread\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据thread_id，对thread进行修改。\n",
    "\n",
    "当前Thread 仅可以修改metadata字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='thread_cdfea60ae56a4d5abebf29080f9a8bf7' object='thread' created_at=1717645470924 metadata=None\n"
     ]
    }
   ],
   "source": [
    "# 依据thread id 更新thread信息\n",
    "thr_update = appbuilder.assistant.threads.update(\n",
    "    thread_id=thread.id,\n",
    "    metadata={\n",
    "        'key': 'value'\n",
    "    }\n",
    ")\n",
    "\n",
    "# 打印更新后的thread信息\n",
    "print(thr_update)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除Thread\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据thread_id删除Thread对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 依据提供的thread_id删除thread\n",
    "appbuilder.assistant.threads.delete(thread_id=thread.id)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
